package com.yfp.tissue.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.bean.BeanUtils;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.common.log.annotation.Log;
import com.yfp.common.log.enums.BusinessType;
import com.yfp.common.security.annotation.InnerAuth;
import com.yfp.common.security.annotation.RequiresPermissions;
import com.yfp.system.api.domain.SysUser;
import com.yfp.tissue.domain.Dto.AreaDto;
import com.yfp.tissue.domain.Dto.CompanyDto;
import com.yfp.tissue.domain.Dto.RouteDTO;
import com.yfp.tissue.domain.entity.Company;
import com.yfp.tissue.domain.entity.Route;
import com.yfp.tissue.domain.vo.*;
import com.yfp.tissue.service.*;
import com.yfp.tissue.domain.vo.AreaVO;
import com.yfp.tissue.domain.vo.CompanyVO;
import com.yfp.tissue.domain.vo.PositionVO;
import com.yfp.tissue.domain.vo.TissueTreeVO;
import com.yfp.tissue.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @PROJECT_NAME: ydyfp
 * @DESCRIPTION:
 * @USER: xiaohuya
 * @DATE: 2023/12/14 15:47
 */


@RestController
@RequestMapping("/company")
@Tag(name = "公司管理")
public class CompanyController extends BaseController {

    @Resource
    private CompanySerivce companySerivce;
    @Resource
    private AreaService areaService;
    @Resource
    private RouteService routeService;
    @Resource
    private PositionService positionService;
    @Resource
    private UserService userService;


    /**
     * 添加公司
     *
     * @param
     */
    @PostMapping("addCompany")
    @Operation(summary = "添加公司")
    @Log(title = "添加公司", businessType = BusinessType.INSERT)

    public R addCompany(@RequestBody CompanyDto companyDto) {
        return companySerivce.addCompany(companyDto);
    }

    /**
     * 公司查询
     *
     * @param
     */
    @GetMapping("listCompany")
    @Operation(summary = "公司查询")
    @RequiresPermissions("tissue:company:list")

    public TableDataInfo queryCompany(
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(value = "bizLicenceBeginTime", required = false) Date bizLicenceBeginTime,
            @RequestParam(value = "bizLicenceEndTime", required = false) Date bizLicenceEndTime,
            @RequestParam(value = "contractBeginDate", required = false) Date contractBeginDate,
            @RequestParam(value = "contractEndDate", required = false) Date contractEndDate,
            @RequestParam(value = "cooperationMode", required = false) String cooperationMode,
            @RequestParam(value = "enabled", required = false) String enabled,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "paymentCode", required = false) String paymentCode,
            @RequestParam(value = "parentId", required = false) String parentId
    ) {
        startPage();
        CompanyDto companyDto = new CompanyDto();
        companyDto.setAddress(address);
        companyDto.setBizLicenceEndTime(bizLicenceEndTime);
        companyDto.setBizLicenceBeginTime(bizLicenceBeginTime);
        companyDto.setContractBeginDate(contractBeginDate);
        companyDto.setContractEndDate(contractEndDate);
        companyDto.setCooperationMode(cooperationMode);
        companyDto.setEnabled(enabled);
        companyDto.setName(name);
        companyDto.setParentId(parentId);
        companyDto.setPaymentCode(paymentCode);
        return getDataTable(companySerivce.queryCompany(companyDto));
    }

    /**
     * 公司条件
     */
    @GetMapping("conditionsCompany")
    @Operation(summary = "条件查询")
    @RequiresPermissions("tissue:company:query")

    public R queryParen(String companyId) {
        return companySerivce.queryParen(companyId);
    }

    /**
     * 查询单个公司
     */
    @GetMapping("oneConditions")
    @Operation(summary = "查询单个公司")
    @RequiresPermissions("tissue:company:info")

    public R queryOneCompany(String companyId) {
        return companySerivce.queryOneCompany(companyId);
    }

    /**
     * 修改公司
     */
    @PostMapping("updateCompany")
    @Operation(summary = "修改公司")
    @Log(title = "修改公司", businessType = BusinessType.UPDATE)
    @RequiresPermissions("tissue:company:edit")

    public R updateCompany(@RequestBody CompanyDto companyDto) {
        return companySerivce.updateCompany(companyDto);
    }

    /**
     * 禁用
     */
    @GetMapping("companyProhibit")
    @Operation(summary = "禁用")
    @Log(title = "禁用", businessType = BusinessType.UPDATE)
    @RequiresPermissions("tissue:company:dis")
    public R addQuery(String id, String enabled) {
        return areaService.prohibit(id, "公司", enabled);

    }

    @GetMapping("/tissueTree")
    @Operation(summary = "查询组织树")
    @Log(title = "查询组织树", businessType = BusinessType.UPDATE)
    public R tissueTree(String cityName) {
        List<CompanyVO> companyVOS = companySerivce.findList(cityName);
        List<TissueTreeVO> tissueTreeVOS = toTissueTreeVO(companyVOS);
        AreaDto areaDto = null;
        for (TissueTreeVO companyVO : tissueTreeVOS) {
            areaDto = new AreaDto();
            areaDto.setCompanyId(companyVO.getId());
            List<AreaVO> areaVOS = areaService.findList(areaDto);
            List<TissueTreeVO> tissueTreeVOS1 = toTissueTreeVO(areaVOS);
            RouteDTO routeDTO = null;
            for (TissueTreeVO areaVO : tissueTreeVOS1) {
                routeDTO = new RouteDTO();
                routeDTO.setAreaId(areaVO.getId());
                List<Route> routes = routeService.findList(routeDTO);
                List<TissueTreeVO> tissueTreeVOS2 = toTissueTreeVO(routes);
                List<TissueTreeVO> routeVOS = new ArrayList<>();
                RouteDTO routeDTO1 = null;
                for (TissueTreeVO route : tissueTreeVOS2) {
                    routeDTO1 = new RouteDTO();
                    routeDTO1.setRouteId(route.getId());
                    System.err.println(route.getId());
                    List<PositionVO> positionVOS = positionService.findList(routeDTO1);
                    List<TissueTreeVO> tissueTreeVO3 = toTissueTreeVO(positionVOS);
                    route.setChildren(tissueTreeVO3);
                    routeVOS.add(route);
                }
                areaVO.setChildren(routeVOS);
            }
            companyVO.setChildren(tissueTreeVOS1);
        }
        return R.ok(tissueTreeVOS);
    }

    public List<TissueTreeVO> toTissueTreeVO(List datas) {
        if (datas.isEmpty()) {
            return new ArrayList<>();
        }
        List<TissueTreeVO> tissueTreeVOS = new ArrayList<>();
        TissueTreeVO tissueTreeVO = null;
        Object data = datas.get(0);
        if (data.getClass().equals(PositionVO.class)) {
            PositionVO positionVO = null;
            for (Object o : datas) {
                positionVO = (PositionVO) o;
                tissueTreeVO = new TissueTreeVO();
                tissueTreeVO.setId(positionVO.getPositionId());
                tissueTreeVO.setName(positionVO.getPositionName());
                if (null != positionVO.getOperationUserId()) {
                    SysUser sysUser = userService.findById(positionVO.getOperationUserId());
                    tissueTreeVO.setUserId(positionVO.getOperationUserId());
                    tissueTreeVO.setUserName(sysUser.getUserName());
                }

                tissueTreeVOS.add(tissueTreeVO);
            }
        } else if (data.getClass().equals(CompanyVO.class)) {
            CompanyVO companyVO = null;
            for (Object o : datas) {
                companyVO = (CompanyVO) o;
                tissueTreeVO = new TissueTreeVO();
                tissueTreeVO.setId(companyVO.getCompanyId());
                tissueTreeVO.setName(companyVO.getName());
                tissueTreeVOS.add(tissueTreeVO);
            }
        } else if (data.getClass().equals(AreaVO.class)) {
            AreaVO areaVO = null;
            for (Object o : datas) {
                areaVO = (AreaVO) o;
                tissueTreeVO = new TissueTreeVO();
                tissueTreeVO.setId(areaVO.getAreaId());
                tissueTreeVO.setName(areaVO.getName());
                tissueTreeVOS.add(tissueTreeVO);
            }
        } else if (data.getClass().equals(Route.class)) {
            Route route = null;
            for (Object o : datas) {
                route = (Route) o;
                tissueTreeVO = new TissueTreeVO();
                tissueTreeVO.setId(route.getRouteId());
                tissueTreeVO.setName(route.getName());
                tissueTreeVOS.add(tissueTreeVO);
            }
        }
        return tissueTreeVOS;
    }
    @PostMapping("/add")
    @Operation(summary = "审核通过添加公司")
    public R add(@RequestBody Company company){

        companySerivce.add(company);

        return R.ok();
    }
    @GetMapping("/findByCompanyAccount")
    @Operation(summary = "根据公司账号获取公司信息")
    public R findByCompanyAccount(String companyAccount){
        Company company = companySerivce.findByCompanyAccount(companyAccount);
        return R.ok(company);
    }

    @InnerAuth
    @DeleteMapping("/del")
    @Operation(summary = "根据公司账号获取公司信息")
    public R del(String id){
        companySerivce.del(id);
        return R.ok();
    }
}